import tkinter as tk
from tkinter import messagebox
#
from tkinter import *
from tkinter import messagebox as tkMessageBox
from tkinter import simpledialog

InputValue = ""

# Tk().withdraw()
# Tk().wm_attributes('-topmost',1)


# 简单对话框，包括字符、整数和浮点数


def input_str():
    r = simpledialog.askstring('字符录入', '请输入字符', initialvalue='hello world!')
    if r:
        print(r)
        tk.label['text'] = '输入的是：' + r


def input_int():
    r = simpledialog.askinteger('整数录入', '请输入整数', initialvalue=100)
    if r:
        print(r)
        tk.label['text'] = '输入的是：' + str(r)


def input_float():
    r = simpledialog.askfloat('浮点数录入', '请输入浮点数', initialvalue=1.01)
    if r:
        print(r)
        tk.label['text'] = '输入的是：' + str(r)

def gogo():
    root = tk.Tk()
    root.title('对话框')
    root.geometry('300x100+300+300')

    label = tk.Label(root, text='输入对话框，包括字符、整数和浮点数', font='宋体 -14', pady=8)
    label.pack()

    frm = tk.Frame(root)
    btn_str = tk.Button(frm, text='字符', width=6, command=input_str)
    btn_str.pack(side=tk.LEFT)
    btn_int = tk.Button(frm, text='整数', width=6, command=input_int)
    btn_int.pack(side=tk.LEFT)
    btn_int = tk.Button(frm, text='浮点数', width=6, command=input_float)
    btn_int.pack(side=tk.LEFT)
    frm.pack()

    root.mainloop()


class MyCollectApp(tk.Toplevel):  # 重点
    def __init__(self,title ='验证码',msg = '是否继续以下业务 ?'):
        super().__init__()  # 重点

        self.title(title)
        self.sfield = title
        self.msg = msg
        self.setupUI()

    def setupUI(self):
        row1 = tk.Frame(self)

        row1.pack(fill="x")
        l1 = tk.Label(row1, text="%s ："%self.sfield, height=2, width=10)
        l1.pack(side=tk.LEFT)  # 这里的side可以赋值为LEFT  RTGHT TOP  BOTTOM

        self.xls_text = tk.StringVar()
        tk.Entry(row1, textvariable=self.xls_text).pack(after = l1)

        row2 = tk.Frame(self)
        row2.pack(fill="x")
        tk.Button(row2, text="点击确认", command=self.on_click).pack(side=tk.BOTTOM)

    def on_click(self):
        # print(self.xls_text.get().lstrip())

        global InputValue
        InputValue = self.xls_text.get().lstrip()
        if len(InputValue) == 0:  # print("用户名必须输入!")
            messagebox.showwarning(title='系统提示', message='请输入%s!' % self.sfield)
            return False
        self.quit()
        self.destroy()
        # print("%s：%s" % (self.sfield,UserName))


def center_window(win,w, h):
    # 获取屏幕 宽、高
    ws = win.winfo_screenwidth()
    hs = win.winfo_screenheight() # 计算 x, y 位置
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    win.geometry('%dx%d+%d+%d' % (w, h, x, y))

def inputBox(stitle):
    app = MyCollectApp(title=stitle)
    center_window(app, 300, 150)
    # app.geometry('400x150')

    # nScreenWid, nScreenHei = app.maxsize()
    # nCurWid = app.winfo_reqwidth()
    # nCurHeight = app.winfo_reqheight()
    # app.geometry( "{}x{}+{}+{}".format(nCurWid, nCurHeight, nScreenWid // 2 - nCurWid // 2, nScreenHei // 2 - nCurHeight // 2))
    app.mainloop()

    return InputValue

def getInput(title, message):
    def return_callback(event):
        # print('quit...')
        root.quit()

    def close_callback():
        tkMessageBox.showinfo('message', '请输入 %s' % title)

    def on_click():
        root.quit()

    root = Tk(className=title)
    root.wm_attributes('-topmost', 1)
    screenwidth, screenheight = root.maxsize()
    width = 300
    height = 100
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(size)
    root.resizable(0, 0)
    lable = tk.Label(root, height=2)
    lable['text'] = message
    lable.pack()
    entry = tk.Entry(root)
    entry.bind('<Return>', return_callback)
    entry.pack()

    tk.Button(root, text="点击确认", command=on_click).pack()

    entry.focus_set()
    root.protocol("WM_DELETE_WINDOW", close_callback)
    root.mainloop()
    str = entry.get()
    root.destroy()
    return str


def showMsgBox(title, message):
    ret = True #返回值

    def return_callback(event):
        # print('quit...')
        root.quit()

    def close_callback():
        tkMessageBox.showinfo('message', '请点击“确认开始”' )

    def on_Yesclick():
        ret = True
        root.quit()
    def on_Noclick():
        ret = False
        root.quit()
    def on_Cancelclick():
        ret = None
        root.quit()


    root = Tk(className=title)
    root.wm_attributes('-topmost', 1)
    screenwidth, screenheight = root.maxsize()
    width = 300
    height = 150
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(size)
    root.resizable(0, 0)

    lable = tk.Label(root, height=5)
    lable['text'] = message
    lable.pack(side=tk.TOP)


    frm = tk.Frame(root)
    bb = tk.Button(frm, text="确认开始",width=8, command=on_Yesclick).pack(side=tk.LEFT)
    # tk.Button(frm, text="否",width=6, command=on_Noclick).pack(side=tk.LEFT)
    # tk.Button(frm, text="取消",width=6, command=on_Cancelclick).pack(side=tk.LEFT)
    frm.pack()

    frm.focus_set()
    root.protocol("WM_DELETE_WINDOW", close_callback)
    root.mainloop()
    # str = entry.get()
    root.destroy()
    return ret


def inputMsgBox(title, message,input1='  验证手机号',input2='手机登录密码',value1='',value2=''):
    ret = True #返回值

    def return_callback(event):
        # print('quit...')
        root.quit()

    def close_callback():
        tkMessageBox.showinfo('message', '请点击“确认开始”' )

    def on_Yesclick():
        ret = True
        root.quit()
    def on_Noclick():
        ret = False
        root.quit()
    def on_Cancelclick():
        ret = None
        root.quit()


    root = Tk(className=title)
    root.wm_attributes('-topmost', 1)
    screenwidth, screenheight = root.maxsize()
    width = 300
    height = 220
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(size)
    root.resizable(0, 0)

    lable = tk.Label(root, height=5)
    lable['text'] = message
    lable.pack(side=tk.TOP)

    var1 = StringVar(value=value1)
    frm1 = tk.Frame(root)
    lable1 = tk.Label(frm1, height=2)
    lable1['text'] = '   %s：'%input1
    lable1.pack(side=tk.LEFT)
    entry1 = tk.Entry(frm1,textvariable=var1)
    # entry1.bind('<Return>', return_callback)
    entry1.pack(side=tk.RIGHT)
    frm1.pack()


    var2 = StringVar(value=value2)
    frm2= tk.Frame(root)
    lable2 = tk.Label(frm2, height=2)
    lable2['text'] = '%s：'% input2
    lable2.pack(side=tk.LEFT)
    entry2 = tk.Entry(frm2,textvariable=var2)
    # entry2.bind('<Return>', return_callback)
    entry2.pack(side=tk.RIGHT)
    frm2.pack()

    frm = tk.Frame(root)
    bb = tk.Button(frm, text="确认开始",width=8, command=on_Yesclick).pack(side=tk.LEFT)
    # tk.Button(frm, text="否",width=6, command=on_Noclick).pack(side=tk.LEFT)
    # tk.Button(frm, text="取消",width=6, command=on_Cancelclick).pack(side=tk.LEFT)
    frm.pack()

    frm.focus_set()
    root.protocol("WM_DELETE_WINDOW", close_callback)
    root.mainloop()
    ret1 = entry1.get()
    ret2= entry2.get()
    root.destroy()
    return ret1,ret2





if __name__ == '__main__':
    # getInput('系统提示', '请输入短信 验证码：（回车录入）')
    # tkMessageBox.askyesnocancel('提示', '是否开始运营商验证 ？\n 是：请准备输入验证码 \n 否：将等待1分钟再次提示 \n 忽略：退出程序')
    # s = simpledialog.askstring('sdaf', 'asdfasd')
    # # print(s)
    # gogo()
    # exit()

    var_box=True
    # var_box = tk.messagebox.askyesnocancel(title='系统提示', message='是否开始运营商验证 ？\n 是：请准备输入验证码 \n 否：将等待1分钟再次提示 \n 忽略：退出程序')  # 返回'True','False'
    # var_box = showMsgBox(title='系统提示', message='是否开始运营商验证 ？\n 是：请准备输入验证码 \n 否：将等待1分钟再次提示 \n 忽略：退出程序')  # 返回'True','False'
    xx,yy = inputMsgBox(title='系统提示', message='是否开始运营商验证 ？\n \n 请注意：确认后，运营商验证将开始 \n 你需要准备录入手机验证码'
                        ,value1='11111111',value2='aaaaaaaaaaa')  # 返回'True','False'

    print(xx,yy)
    if var_box:
        s = getInput('短信验证码','请准备输入验证码 (回车输入)')

        print(s)



    elif var_box==None:
        print('忽略')
    else:
        print('不做处理')
